package com.lenovo.smarttraffic.ui.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.SupportActivity;
import android.util.Log;
import android.util.SparseArray;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.dao.DaoManager;
import com.lenovo.smarttraffic.database.dao.TitleStrBean;
import com.lenovo.smarttraffic.database.dao.TitleStrBeanDao;
import com.lenovo.smarttraffic.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Fragment工厂类
 */
public class FragmentFactory {
    /**
     * 根据position生产不同的fragment
     *
     * @param position
     * @return
     */
    private static FragmentFactory mFactory = null;
    private String TAG = "FragmentFactory";
    List<String> stringArray = new ArrayList<>();

    //  public static String[] stringArray = {"推荐", "热点", "科技", "汽车资讯"};
    public static FragmentFactory getInstance() {
        if (mFactory == null) {
            mFactory = new FragmentFactory();
        }
        return mFactory;
    }

    //BasePagerAdapter  中实现  fragment  初始化
    public Fragment getFragment(int position) {
        initTitleList();
        SparseArray<Fragment> map = new SparseArray<>();
        Fragment fragment = null;
        if (map.get(position) != null) {
            return map.get(position);
        }
        switch (position) {
            case 0:
                fragment = FirstFragment.onNewBundle(stringArray.get(position));
                break;
            case 1:
                fragment = FirstFragment.onNewBundle(stringArray.get(position));
                break;
            case 2:
                fragment = FirstFragment.onNewBundle(stringArray.get(position));
                break;
            case 3:
                fragment = FirstFragment.onNewBundle(stringArray.get(position));
                break;
            case 4:
                fragment = FirstFragment.onNewBundle(stringArray.get(position));
                break;
            case 5:
                fragment = FirstFragment.onNewBundle(stringArray.get(position));
                break;
            case 6:
                fragment = FirstFragment.onNewBundle(stringArray.get(position));
                break;
        }
        map.put(position, fragment);
        return fragment;
    }

    private void initTitleList() {
        TitleStrBeanDao titleStrBeanDao = DaoManager.getInstance().getDaoSession().getTitleStrBeanDao();
        List<TitleStrBean> list;
        list = titleStrBeanDao.queryBuilder().list();
        List<String> tabArrlist = new ArrayList<>();
        if (list.size() == 0) {
            titleStrBeanDao.insert(new TitleStrBean(null, "推荐"));
            titleStrBeanDao.insert(new TitleStrBean(null, "热点"));
            titleStrBeanDao.insert(new TitleStrBean(null, "科技"));
            titleStrBeanDao.insert(new TitleStrBean(null, "汽车资讯"));
            list = titleStrBeanDao.queryBuilder().list();
        }
        for (int i = 0; i < list.size(); i++) {
            tabArrlist.add(list.get(i).getTitle());
        }
        stringArray = tabArrlist;
    }
}